<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<title>备用金管理</title>
	<meta name="decorator" content="ani"/>
	<style>
		#searchresult
		{
			width: 260px;
			position: absolute;
			z-index: 1;
			overflow: hidden;
			left: 140px;
			top: 71px;
			background: #E0E0E0;
			border-top: none;

		}
		#searchresult1
		{
			width: 260px;
			position: absolute;
			z-index: 1;
			overflow: hidden;
			left: 140px;
			top: 71px;
			background: #E0E0E0;
			border-top: none;

		}

		.line
		{
			font-size: 15px;
			background: #E0E0E0;
			width: 140px;
			padding:0px;
		}
		.line1
		{
			font-size: 15px;
			background: #E0E0E0;
			width: 140px;
			padding:0px;
		}
		.hover
		{
			background: #007ab8;
			width: 140px;
			color: #fff;
		}
		.std
		{
			width: 150px;
		}
	</style>
	<script type="text/javascript">
		var validateForm;
		var $table; // 父页面table表格iD
		var $topIndex;//弹出窗口的 index
		function doSubmit(table, index){//回调函数，在编辑和保存动作时，供openDialog调用提交表单。
		  if(validateForm.form()){
			  $table = table;
			  $topIndex = index;
			  jp.loading();
			  $("#inputForm").submit();
			  return true;
		  }

		  return false;
		}
		function  show() {


			
        }

		$(document).ready(function() {
			validateForm = $("#inputForm").validate({
				submitHandler: function(form){
					jp.post("${ctx}/ap/imprest/save",$('#inputForm').serialize(),function(data){
						if(data.success){
	                    	$table.bootstrapTable('refresh');
	                    	jp.success(data.msg);
	                    	jp.close($topIndex);//关闭dialog

	                    }else{
            	  			jp.error(data.msg);
	                    }
					})
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
			
	        $('#applicationDate').datetimepicker({
				 format: "YYYY-MM-DD HH:00"
		    });
	        $('#receivedDate').datetimepicker({
				 format: "YYYY-MM-DD HH:00"
		    });
		});
		
		function addRow(list, idx, tpl, row){
			$(list).append(Mustache.render(tpl, {
				idx: idx, delBtn: true, row: row
			}));
			$(list+idx).find("select").each(function(){
				$(this).val($(this).attr("data-value"));
			});
			$(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
				var ss = $(this).attr("data-value").split(',');
				for (var i=0; i<ss.length; i++){
					if($(this).val() == ss[i]){
						$(this).attr("checked","checked");
					}
				}
			});
			$(list+idx).find(".form_datetime").each(function(){
				 $(this).datetimepicker({
					 format: "YYYY-MM-DD HH:mm:ss"
			    });
			});
		}
		function delRow(obj, prefix){
			var id = $(prefix+"_id");
			var delFlag = $(prefix+"_delFlag");
			if (id.val() == ""){
				$(obj).parent().parent().remove();
			}else if(delFlag.val() == "0"){
				delFlag.val("1");
				$(obj).html("&divide;").attr("title", "撤销删除");
				$(obj).parent().parent().addClass("error");
			}else if(delFlag.val() == "1"){
				delFlag.val("0");
				$(obj).html("&times;").attr("title", "删除");
				$(obj).parent().parent().removeClass("error");
			}
		}

        function ee(evt) {
            ChangeCoords1(); //控制查询结果div坐标
            var k = window.event ? evt.keyCode : evt.which;
            //输入框的id为txt_search，这里监听输入框的keyup事件
            //不为空 && 不为上箭头或下箭头或回车
            if ($("#applicationDivision").val() != "" && k != 38 && k != 40 && k != 13) {

                $.ajax({
                    type: 'post',
                    async: true, //同步执行，不然会有问题
                    dataType: "json",
                    url: "${ctx}/infor/department/getByName?name="+$("#applicationDivision").val(),
                    //提交的页面/方法
                    error: function (msg) {//请求失败处理函数
                        alert("数据加载失败");
                    },
                    success: function (data) { //请求成功后处理函数。
						/*  var objData = eval("(" + data.userName + ")");   */
                        console.log(data);
                        if (data.length > 0) {
                            var layer = "";
                            layer = "<table id='aa'>";
                            $.each(data, function (idx, item) {
                                layer += "<tr class='line1' id='"+item.id+"'><td class='std'>" + item.name+ "</td></tr>";
                            });
                            layer += "</table>";
                            //将结果添加到div中
                            $("#searchresult").empty();
                            $("#searchresult").append(layer);
                            $(".line1:first").addClass("hover");
                            $("#searchresult").css("display", "");
                            //鼠标移动事件

                            $(".line1").hover(function () {
                                $(".line1").removeClass("hover");
                                $(this).addClass("hover");
                            }, function () {
                                $(this).removeClass("hover");
                                //$("#searchresult").css("display", "none");
                            });
                            //鼠标点击事件
                            $(".line1").click(function () {
                                $("#applicationDivision").val($(this).text());
                                ($(this).attr("id"));
                                $("#searchresult").css("display", "none");
                            });
                        } else {
                            $("#searchresult").empty();
                            $("#searchresult").css("display", "none");
                        }
                    }
                });
            }
            else if (k == 38) {//上箭头
                $('#aa tr.hover').prev().addClass("hover");
                $('#aa tr.hover').next().removeClass("hover");
                $('#applicationDivision').val($('#aa tr.hover').text());
            }
            else if (k == 40) {//下箭头
                $('#aa tr.hover').next().addClass("hover");
                $('#aa tr.hover').prev().removeClass("hover");
                $('#applicationDivision').val($('#aa tr.hover').text());
            }
            else if (k == 13) {//回车
                $('#applicationDivision').val($('#aa tr.hover').text());
                ($('#aa tr.hover').attr("id"));
                $("#searchresult").empty();
                $("#searchresult").css("display", "none");
            }
            else {
                $("#searchresult").empty();
                $("#searchresult").css("display", "none");
            }
        };
        $("#searchresult").bind("mouseleave", function () {
            $("#searchresult").empty();
            $("#searchresult").css("display", "none");
        });

        function ChangeCoords1() {
            //    var left = $("#txt_search")[0].offsetLeft; //获取距离最左端的距离，像素，整型
            //    var top = $("#txt_search")[0].offsetTop + 26; //获取距离最顶端的距离，像素，整型（20为搜索输入框的高度）
            var left = $("#applicationDivision").position().left; //获取距离最左端的距离，像素，整型
            var top = $("#applicationDivision").position().top + 30; ; //获取距离最顶端的距离，像素，整型（20为搜索输入框的高度）
            $("#searchresult").css("left", left + "px"); //重新定义CSS属性
            $("#searchresult").css("top", top + "px"); //同上
        }
        function em(evt) {
            ChangeCoords(); //控制查询结果div坐标
            var k = window.event ? evt.keyCode : evt.which;
            //输入框的id为txt_search，这里监听输入框的keyup事件
            //不为空 && 不为上箭头或下箭头或回车
            if ($("#applicant").val() != "" && k != 38 && k != 40 && k != 13) {

                $.ajax({
                    type: 'post',
                    async: true, //同步执行，不然会有问题
                    dataType: "json",
                    url: "${ctx}/infor/staff/getByStaff?name="+$("#applicant").val(),
                    //提交的页面/方法
                    error: function (msg) {//请求失败处理函数
                        alert("数据加载失败");
                    },
                    success: function (data) { //请求成功后处理函数。
						/*  var objData = eval("(" + data.userName + ")");   */
                        console.log(data);
                        if (data.length > 0) {
                            var layer = "";
                            layer = "<table id='aa'>";
                            $.each(data, function (idx, item) {
                                layer += "<tr class='line' id='"+item.id+"'><td class='std'>" + item.name+ "</td></tr>";
                            });
                            layer += "</table>";
                            //将结果添加到div中
                            $("#searchresult1").empty();
                            $("#searchresult1").append(layer);
                            $(".line:first").addClass("hover");
                            $("#searchresult1").css("display", "");
                            //鼠标移动事件

                            $(".line").hover(function () {
                                $(".line").removeClass("hover");
                                $(this).addClass("hover");
                            }, function () {
                                $(this).removeClass("hover");
                                //$("#searchresult").css("display", "none");
                            });
                            //鼠标点击事件
                            $(".line").click(function () {
                                $("#applicant").val($(this).text());
                                ($(this).attr("id"));
                                $("#searchresult1").css("display", "none");
                            });
                        } else {
                            $("#searchresult1").empty();
                            $("#searchresult1").css("display", "none");
                        }
                    }
                });
            }
            else if (k == 38) {//上箭头
                $('#aa tr.hover').prev().addClass("hover");
                $('#aa tr.hover').next().removeClass("hover");
                $('#applicant').val($('#aa tr.hover').text());
            }
            else if (k == 40) {//下箭头
                $('#aa tr.hover').next().addClass("hover");
                $('#aa tr.hover').prev().removeClass("hover");
                $('#applicant').val($('#aa tr.hover').text());
            }
            else if (k == 13) {//回车
                $('#applicant').val($('#aa tr.hover').text());
                ($('#aa tr.hover').attr("id"));
                $("#searchresult1").empty();
                $("#searchresult1").css("display", "none");
            }
            else {
                $("#searchresult1").empty();
                $("#searchresult1").css("display", "none");
            }
        };
        $("#searchresult1").bind("mouseleave", function () {
            $("#searchresult1").empty();
            $("#searchresult1").css("display", "none");
        });
        function ChangeCoords() {
            //    var left = $("#txt_search")[0].offsetLeft; //获取距离最左端的距离，像素，整型
            //    var top = $("#txt_search")[0].offsetTop + 26; //获取距离最顶端的距离，像素，整型（20为搜索输入框的高度）
            var left = $("#applicant").position().left; //获取距离最左端的距离，像素，整型
            var top = $("#applicant").position().top + 30; ; //获取距离最顶端的距离，像素，整型（20为搜索输入框的高度）
            $("#searchresult1").css("left", left + "px"); //重新定义CSS属性
            $("#searchresult1").css("top", top + "px"); //同上
        }

        function bbb() {
           var a= $("#money").val();
           var b= $("#oilMoney").val();
            if(a==null||a==""){
                a="0";
            }
            if(b==null||b==""){
                b="0";
            }
            $("#totalMoney").val(parseInt(a)+parseInt(b));

        }
        function zh() {
            var xj=$("#accountMoney").val();
            var yk=$("#oilsMoney").val();
            if(xj==""){
                xj="0"
			}
            if(yk==""){
                yk="0"
            }
            var a= $("#money").val();
            var b= $("#oilMoney").val();
            if(a==null||a==""){
                a="0";
            }
            if(b==null||b==""){
                b="0";
            }
            if(parseFloat(xj)>parseFloat(a)){
                jp.error("到账金额不应大于申请金额");
                $("#accountMoney").val("");
                return;
			}
            if(parseFloat(yk)>parseFloat(b)){
                jp.error("到账金额不应大于申请金额");
                $("#oilsMoney").val("");
                return;
            }
            $("#paymentMoney").val(parseFloat(xj)+parseFloat(yk));
			
        }
        function ChangeCoords5(id) {
            //    var left = $("#txt_search")[0].offsetLeft; //获取距离最左端的距离，像素，整型
            //    var top = $("#txt_search")[0].offsetTop + 26; //获取距离最顶端的距离，像素，整型（20为搜索输入框的高度）
            var left = $("#"+id).position().left; //获取距离最左端的距离，像素，整型
            var top = $("#"+id).position().top + 30; ; //获取距离最顶端的距离，像素，整型（20为搜索输入框的高度）
            $("#searchresult5").css("left", left + "px"); //重新定义CSS属性
            $("#searchresult5").css("top", top + "px"); //同上
        }
        function se(evt,id,idx) {
            ChangeCoords5(id); //控制查询结果div坐标

            var k = window.event ? evt.keyCode : evt.which;
            //输入框的id为txt_search，这里监听输入框的keyup事件
            //不为空 && 不为上箭头或下箭头或回车
            if ($("#"+id).val() != "" &&  k == 13) {
                $.ajax({
                    type: 'post',
                    async: true, //同步执行，不然会有问题
                    dataType: "json",
                    url: "${ctx}/ap/card/getcardByName?name="+$("#"+id).val(),   //提交的页面/方法
                    error: function (msg) {//请求失败处理函数
                        alert("数据加载失败");
                    },
                    success: function (data) { //请求成功后处理函数。
						/*  var objData = eval("(" + data.userName + ")");   */
                        console.log(data);
                        if (data.length > 0) {
                            var layer = "";
                            layer = "<table id='aa'>";
                            $.each(data, function (idx, item) {
                                layer += "<tr class='line' id='"+item.type+"'><td class='std'>" + item.number+ "</td></tr>";
                            });
                            layer += "</table>";

                            //将结果添加到div中
                            $("#searchresult5").empty();
                            $("#searchresult5").append(layer);
                            $(".line1:first").addClass("hover");
                            $("#searchresult5").css("display", "");
                            //鼠标移动事件

                            $(".line").hover(function () {
                                $(".line").removeClass("hover");
                                $(this).addClass("hover");
                            }, function () {
                                $(this).removeClass("hover");
                                //$("#searchresult").css("display", "none");
                            });
                            //鼠标点击事件
                            $(".line").click(function () {
                                $("#"+id).val($(this).text());
                                $("#paymentWayList"+idx+"_name").val($(this).attr("id"));
                                $("#searchresult5").css("display", "none");
                            });
                        } else {
                            $("#searchresult5").empty();
                            $("#searchresult5").css("display", "none");
                        }
                    }
                });
            }
            else if (k == 38) {//上箭头
                $('#aa tr.hover').prev().addClass("hover");
                $('#aa tr.hover').next().removeClass("hover");
                $("#"+id).val($('#aa tr.hover').text());
            }
            else if (k == 40) {//下箭头
                $('#aa tr.hover').next().addClass("hover");
                $('#aa tr.hover').prev().removeClass("hover");
                $("#"+id).val($('#aa tr.hover').text());
            }
            else {
                $("#searchresult5").empty();
                $("#searchresult5").css("display", "none");
            }
        }
	</script>
</head>
<body class="bg-white">


		<form:form id="inputForm" modelAttribute="imprest" action="${ctx}/ap/imprest/save" method="post" class="form-horizontal"
		>
		<form:hidden path="id"/>
		<sys:message content="${message}"/>
			<input type="hidden" name="projectName" value="${imprest.projectName}"/>
			<input type="hidden" name="ln" value="${ln}"/>
		<table class="table table-bordered">
		   <tbody>
				<tr>
					<td class="width-15 active"><label class="pull-right">申请部门：</label></td>
					<td class="width-35">
						<input id="applicationDivision" readonly="readonly"  name="applicationDivision"   value="${imprest.applicationDivision}" type="text"  class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">申请人：</label></td>
					<td class="width-35">
						<input id="applicant"   name="applicant"  onkeyup="em(event)" readonly="readonly"  type="text" value="${imprest.applicant}" class="form-control "/>
						<div id="searchresult1" style="display: none;"></div>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">申请理由：</label></td>
					<td class="width-35">
						<form:select path="reason" class="form-control ">
							<form:options items="${fns:getDictList('reson')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
					</td>
					<td class="width-15 active"><label class="pull-right">备用金单据号：</label></td>
					<td class="width-35">
						<form:input path="number" readonly="true" htmlEscape="false"    class="form-control "/>
					</td>



				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right"><span style="color: red">*</span>申请日期：</label></td>
					<td class="width-35">
						<p class="input-group">
						<div class='input-group form_datetime' id='applicationDate'>
							<input type='text'  name="applicationDate" class="form-control required"   value="<fmt:formatDate value="${imprest.applicationDate}" pattern="yyyy-MM-dd HH:mm:ss"/>"/>
							<span class="input-group-addon">
			                        <span class="glyphicon glyphicon-calendar"></span>
			                    </span>
						</div>
						</p>
					</td>
					<td class="width-15 active"><label class="pull-right"><span style="color: red">*</span>期望到账日期：</label></td>
					<td class="width-35">
						<p class="input-group">
						<div class='input-group form_datetime' id='receivedDate'>
							<input type='text'  name="receivedDate" class="form-control required"  value="<fmt:formatDate value="${imprest.receivedDate}" pattern="yyyy-MM-dd HH:mm:ss"/>"/>
							<span class="input-group-addon">
			                        <span class="glyphicon glyphicon-calendar"></span>
			                    </span>
						</div>
						</p>
					</td>

				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right"><span style="color: red">*</span>申请金额：</label></td>
					<td class="width-35">
						<form:input path="totalMoney" htmlEscape="false"    class="form-control required"/>
					</td>
					<td class="width-15 active"><label class="pull-right"><span style="color: red">*</span>申请类别：</label></td>
					<td class="width-35">
						<form:select path="money" class="form-control required ">
							<form:options items="${fns:getDictList('moneyType')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>

					</td>
				</tr>
				<tr>

					<td class="width-15 active" ><label class="pull-right"  >单据状态：</label></td>

					<td class="width-35" >
						<form:input readonly="true" path="documentStatus" htmlEscape="false"    class="form-control "/>
					</td
				</tr>

		   </tbody>
		</table>
			<div <c:if test="${imprest.documentStatus != '已到账' &&ln!=1}">hidden="hidden"</c:if>>
				<table class="table table-bordered">
					<tbody>
					<tr>
						<td class="width-15 active"><label class="pull-right">到账金额：</label></td>
						<td class="width-35">
							<form:input path="paymentMoney" htmlEscape="false"    class="form-control "/>
						</td>


						<td class="width-15 active"><label class="pull-right">付款人：</label></td>
						<td class="width-35">
							<form:input path="payer" htmlEscape="false"    class="form-control "/>
						</td>
					</tr>
					<tr>
						<td class="width-15 active"><label class="pull-right">接收人：</label></td>
						<td class="width-35">
							<form:input path="recipient" htmlEscape="false"    class="form-control "/>
						</td>
						<td class="width-15 active"><label class="pull-right">备注信息：</label></td>
						<td class="width-35">
							<form:input path="remarks"  htmlEscape="false"     class="form-control "/>
						</td>
					</tr>
					</tbody>
				</table>

			</div>

			<div class="tabs-container">
			<ul class="nav nav-tabs">
				<li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">支付方式：</a>
				</li>
			</ul>
			<div class="tab-content">
				<div id="tab-1" class="tab-pane fade in  active">
					<a class="btn btn-white btn-sm" onclick="addRow('#paymentWayList', paymentWayRowIdx, paymentWayTpl);paymentWayRowIdx = paymentWayRowIdx + 1;" title="新增"><i class="fa fa-plus"></i> 新增</a>
					<table class="table table-striped table-bordered table-condensed">
						<thead>
						<tr>
							<th class="hide"></th>
							<th width="10%">支付方式</th>
							<th>号码</th>
							<th>名称</th>
							<th>金额</th>
							<th>其他信息</th>
							<th>备注信息</th>
							<th width="10">&nbsp;</th>
						</tr>
						</thead>
						<tbody id="paymentWayList">
						</tbody>
					</table>
					<script type="text/template" id="paymentWayTpl">//<!--
				<tr id="paymentWayList{{idx}}">
					<td class="hide">
						<input id="paymentWayList{{idx}}_id" name="paymentWayList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
						<input id="paymentWayList{{idx}}_delFlag" name="paymentWayList[{{idx}}].delFlag" type="hidden" value="0"/>
					</td>

					<td>
					<input id="paymentWayList{{idx}}_paymentsWays" readonly="readonly"  name="paymentWayList[{{idx}}].paymentsWays" value="油卡" class="form-control m-b  ">


					</td>

<td>
						<input id="paymentWayList{{idx}}_number" autocomplete="off" name="paymentWayList[{{idx}}].number" type="text" value="{{row.number}}"  onkeyup="se(event,this.id,'{{idx}}')"   class="form-control "/>
					<div id="searchresult5" style="display: none;"></div>
					</td>

					<td>
						<input id="paymentWayList{{idx}}_name" name="paymentWayList[{{idx}}].name" type="text" value="{{row.name}}"    class="form-control "/>
					</td>
					<td>
						<input id="paymentWayList{{idx}}_money" name="paymentWayList[{{idx}}].money" type="text" value="{{row.money}}"    class="form-control "/>
					</td>








					<td>
						<input id="paymentWayList{{idx}}_otherInformation" name="paymentWayList[{{idx}}].otherInformation" type="text" value="{{row.otherInformation}}"    class="form-control "/>
					</td>





					<td>
						<input id="paymentWayList{{idx}}_remarks" name="paymentWayList[{{idx}}].remarks"   type="text"  value="{{row.remarks}}"   class="form-control "/>
					</td>

					<td class="text-center" width="10">
						{{#delBtn}}<span class="close" onclick="delRow(this, '#paymentWayList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
					</td>
				</tr>//-->
					</script>
					<script type="text/javascript">
                        var paymentWayRowIdx = 0, paymentWayTpl = $("#paymentWayTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                        $(document).ready(function() {
                            var data = ${fns:toJson(imprest.paymentWayList)};
                            for (var i=0; i<data.length; i++){
                                addRow('#paymentWayList', paymentWayRowIdx, paymentWayTpl, data[i]);
                                paymentWayRowIdx = paymentWayRowIdx + 1;
                            }
                        });
					</script>
				</div>
			</div>
		</div>


		</form:form>


</body>

</html>